package com.mobi.Survey.lib.model;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.mobi.Survey.lib.activities.Messages;
import com.mobi.Survey.lib.utils.Helper;

/**
 * 
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * DefaultSurveyXmlParser.java Xml parser of default surveys
 */
public class DefaultSurveyXmlParser extends DefaultHandler
{

	private static final String SurveyKey = "SurveyKey";
	private static final String SurveyId = "SurveyId";
	private static final String SurveyDiscription = "SurveyDiscription";
	private static final String SubscriptionId = "SubscriptionId";
	private static final String FirstName = "FirstName";
	private static final String LastName = "LastName";
	private static final String Email = "Email";
	private static final String ICON_URL = "iconUrl";

	private static final String CompanyName = "CompanyName";
	private static final String Phone = "Phone";
	private static final String ContactEmail = "ContactEmail";
	private static final String Website = "Website";
	private static final String SinglePage = "SinglePage";
	private static final String TxtBtnStartSurvey = "TxtBtnStartSurvey";
	private static final String TxtBtnCorrectAnswer = "TxtBtnCorrectAnswer";
	private static final String TxtBtnSurveyResult = "TxtBtnSurveyResult";
	private static final String SurveyDetail = "SurveyDetail";
	private static final String ControlType = "ControlType";
	private static final String ControlText = "ControlText";
	private static final String ControlParameter = "ControlParameter";
	private static final String ControlResult = "ControlResult";
	private static final String Mendatory = "Mendatory";
	private static final String SurveyDetails = "SurveyDetails";

	private static final String StreetNumber = "StreetNumber";
	private static final String StreetName = "StreetName";
	private static final String City = "City";
	private static final String State = "State";
	private static final String Country = "Country";
	private static final String Zip = "Zip";

	List<SurveyPojo> surveys;
	SurveyPojo sp;
	SurveyDetailPojo sdp;
	List<SurveyDetailPojo> sdps;
	SubscriptionPojo sub;
	StringBuffer value;
	long id;
	boolean inDetail;
	String surveyKey;

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException
	{
		value.append(new String(ch, start, length).trim().replaceAll("\t", "")
				.replaceAll("\n", ""));
	}

	private SAXParser saxParser;

	public DefaultSurveyXmlParser()
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try
		{
			saxParser = factory.newSAXParser();
		} catch (ParserConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void parse(String xmlUrl) throws MalformedURLException, IOException,
			SAXException
	{
		InputStream isr = new URL(xmlUrl).openStream();
		saxParser.parse(isr, this);
	}

	@Override
	public void endDocument() throws SAXException
	{

	}

	@Override
	public void endElement(String uri, String localName, String qNames)
			throws SAXException
	{
		if (qNames.equalsIgnoreCase(SurveyDetail))
		{
			sdps.add(sdp);
		} else if (qNames.equalsIgnoreCase(SurveyDetails))
		{
			inDetail = false;
			sp.setSurveyDetail(sdps);
		} else if (qNames.equalsIgnoreCase(SurveyKey))
		{
			sp.setSubscription(sub);
			sub = null;
			sp.setSurveyKey(surveyKey);
			surveys.add(sp);
		} else if (qNames.equalsIgnoreCase(SurveyId) && !inDetail)
		{

			id = Helper.toLong(value.toString());
			if (id == 0)
				throw new RuntimeException(Messages.SURVEY_WILL_NOT_DOWNLOAD
						+ id);
			sp.setId(id);
		} else if (qNames.equalsIgnoreCase(SurveyDiscription))
		{
			sp.setSurveyDiscription(value.toString());
		} else if (qNames.equalsIgnoreCase(SubscriptionId) && !inDetail)
		{
			long id = Helper.toInt(value.toString());
			sp.setSubscriptionId(id);
			sub.setSubsId(id);
		} else if (qNames.equalsIgnoreCase(FirstName))
		{

			sub.setfName(value.toString());
		} else if (qNames.equalsIgnoreCase(LastName))
		{

			sub.setlName(value.toString());
		} else if (qNames.equalsIgnoreCase(Email))
		{
			sp.setSubsEmail(value.toString());
			sub.setEmail(value.toString());
		} else if (qNames.equalsIgnoreCase(StreetNumber))
		{

			sp.getAddress().setStreetNumber(value.toString());
		} else if (qNames.equalsIgnoreCase(StreetName))
		{

			sp.getAddress().setStreetName(value.toString());
		} else if (qNames.equalsIgnoreCase(City))
		{

			sp.getAddress().setCity(value.toString());
		} else if (qNames.equalsIgnoreCase(State))
		{

			sp.getAddress().setState(value.toString());
		} else if (qNames.equalsIgnoreCase(Country))
		{

			sp.getAddress().setCountry(value.toString());
		} else if (qNames.equalsIgnoreCase(Zip))
		{

			sp.getAddress().setZip(value.toString());
		}

		else if (qNames.equalsIgnoreCase(ICON_URL))
		{
			sp.setIconAddress(value.toString());
		} else if (qNames.equalsIgnoreCase(CompanyName))
		{

			sp.setCompanyName(value.toString());
		} else if (qNames.equalsIgnoreCase(Phone))
		{

			sp.setPhone(value.toString());
		} else if (qNames.equalsIgnoreCase(ContactEmail))
		{

			sp.setContactEmail(value.toString());
		} else if (qNames.equalsIgnoreCase(Website))
		{

			sp.setWebsite(value.toString());
		} else if (qNames.equalsIgnoreCase(SinglePage))
		{ 

			sp.setShowAllControls(Helper.toBoolean(value.toString()));
		}
//		else if (qNames.equalsIgnoreCase(TxtBtnStartSurvey))
//		{
//
//			sp.setTxtBtnSurveyResult(value.toString());
//		}
		else if (qNames.equalsIgnoreCase(TxtBtnCorrectAnswer))
		{
			if(value.toString().length()>0)
			sp.setTxtBtnCorrectAnswer(value.toString());
		} else if (qNames.equalsIgnoreCase(TxtBtnSurveyResult))
		{
			if(value.toString().length()>0)
			sp.setTxtBtnSurveyResult(value.toString());
		} else if (qNames.equalsIgnoreCase(ControlType))
		{

			sdp.setControlType(Helper.toByte(value.toString()));
		} else if (qNames.equalsIgnoreCase(ControlText))
		{

			sdp.setControlText(value.toString());
		} else if (qNames.equalsIgnoreCase(ControlParameter))
		{

			sdp.setControlParameter1(value.toString());
		} else if (qNames.equalsIgnoreCase(ControlResult))
		{

			sdp.setActualValue(value.toString());
		} else if (qNames.equalsIgnoreCase(Mendatory))
		{
			boolean mend = value.toString().equalsIgnoreCase(true + "");
			sdp.setMendatory(mend);
		}

	}

	@Override
	public void startDocument() throws SAXException
	{

		surveys = new ArrayList<SurveyPojo>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{
		value = null;

		value = new StringBuffer();

		if (localName.equalsIgnoreCase(SurveyDetails))
		{
			inDetail = true;
			sdps = new ArrayList<SurveyDetailPojo>();
		} else if (localName.equalsIgnoreCase(SurveyDetail))
		{
			sdp = new SurveyDetailPojo();
			sdp.setSurveyId(sp.getId());
			sdp.setSubscriptionId(sp.getSubscriptionId());
		} else if (localName.equalsIgnoreCase(SurveyKey))
		{
			sub = new SubscriptionPojo();
			sp = new SurveyPojo();
			surveyKey = attributes.getValue("key");
			if (surveyKey == null || surveyKey.trim().length() == 0)
				throw new RuntimeException(Messages.INVALID_KEY);
			sp.setSurveyKey(surveyKey.trim());
		}

	}

	/**
	 * 
	 * @return returns Surveys after parsing xml from remote server
	 */
	public List<SurveyPojo> getDefaultSurveys()
	{

		return surveys;
	}

}
